package fr.gdi.android.news;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.util.Log;
import fr.gdi.android.news.utils.Constants;
import fr.gdi.android.news.utils.Constants.Actions;
import fr.gdi.android.news.utils.PreferenceUtils;
import fr.gdi.android.news.utils.Utils;

public class UpdateService extends Service
{
    public static PendingIntent serviceIntent = null;
    
    private WakeLock wakeLock;
    
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        if (Constants.USE_EMULATOR)
        {
            Log.d(this.getClass().getName(), "startCommand");
        }
        
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());
        wakeLock.acquire();
        
        if (Utils.isOnline(getApplicationContext()))
        {
            if ( intent != null && intent.getExtras() != null )
            {
                int widgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
                update(getApplicationContext(), widgetId);
            }
        }
        
        stopSelf(startId);
        return START_STICKY;
    }
    
    public void onDestroy()
    {
        super.onDestroy();
        wakeLock.release();
    }
    
    public static void update(Context context, Integer widgetId)
    {
        Intent updateIntent = new Intent(context, WidgetUpdater.class);
        updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
        updateIntent.setAction(Actions.ACTION_UPDATE);
        context.sendBroadcast(updateIntent);
    }
    
    public static final void updateAll(Context ctx)
    {
        int[] appWidgetIds = Utils.getAllWidgetIds(ctx);
        for (int appWidgetId : appWidgetIds)
        {
            update(ctx, appWidgetId);
        }
    }
    
    public static void register(Context context)
    {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        
        if (serviceIntent != null) alarmManager.cancel(serviceIntent);
        
        final int updateInterval = PreferenceUtils.getUpdateInterval(context) * 60000;
        
        if (Constants.USE_EMULATOR)
        {
            Log.d(UpdateService.class.getName(), "update interval = " + updateInterval);
        }
        
        if (updateInterval != 0)
        {
            if (serviceIntent == null)
            {
                Intent intent = new Intent(context, UpdateService.class);
                serviceIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            }
            
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + updateInterval, updateInterval,
                    serviceIntent);
        }
    }
    
    public static void removeService(Context context)
    {
        if (serviceIntent != null)
        {
            ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).cancel(serviceIntent);
            serviceIntent = null;
        }
    }
    
}
